package com.example.healkep;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodSimpleListActivity extends AppCompatActivity {

    private ListView mListView;
    private SimpleAdapter mSimpleAdapter;
    private List<Map<String, Object>> mList;

    private int[] imgs = {
            R.drawable.test1,
            R.drawable.test2,
            R.drawable.test3,
            R.drawable.test4,
            R.drawable.test5,
            R.drawable.test6,
            R.drawable.test7,
            R.drawable.test8,
            R.drawable.test9
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food_activity_simple_list);
        mListView = findViewById(R.id.lv);

        mList = new ArrayList<>();

        for (int i = 0; i < 50; i++) {
            Map<String, Object> map = new HashMap();
            map.put("img", imgs[i % imgs.length]);
            map.put("title", "这是标题" + i);
            map.put("content", "这是内容" + i);

            mList.add(map);
        }

        mSimpleAdapter = new SimpleAdapter(this,
                mList,
                R.layout.food_list_item_layout,
                new String[]{"img", "title", "content"},
                new int[]{R.id.iv_img, R.id.tv_title, R.id.tv_content}
        );

        mListView.setAdapter(mSimpleAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = mList.get(position);
                String title = (String) map.get("title");
                Toast.makeText(FoodSimpleListActivity.this, "你点击了" + position + title, Toast.LENGTH_SHORT).show();

            }
        });

    }
}